//
//  MSFinanceController.m
//  觅尚
//
//  Created by No.8 on 2019/4/5.
//  Copyright © 2019 No.8. All rights reserved.
//

#import "MSFinanceController.h"
#import "MSTitleButton.h"
#import "MSFinancialRoomManagementController.h"
#import "MSOtherEntriesController.h"
#import "MSBusinessReporController.h"
#import "MSFinancialSettlementController.h"
#import "MSBindingRelationsController.h"
#import "MSFinancialAdministrationController.h"
#import "MSFinanciaActivateCardController.h"
#import "MSCaiwuController.h"
#import "AppDelegate.h"


@interface MSFinanceController ()
/** <#name#>*/
@property(nonatomic,strong)NSString *updateStatus;



@property(nonatomic,strong)NSString *updateDescription;


/** <#name#>*/
@property(nonatomic,strong)NSString *versionStatus;


/** 遮盖层*/
@property(nonatomic,strong)UIView *backView;


/** 弹出View*/
@property(nonatomic,strong)UIView *showView;


/** 遮盖层*/
@property(nonatomic,strong)UIView *backView1;


/** 弹出View*/
@property(nonatomic,strong)UIView *showView1;

/** <#name#>*/
//@property(nonatomic,strong)NSString *versionStatus;
@end

@implementation MSFinanceController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    self.navigationController.navigationBar.hidden = YES;
    
    self.view.backgroundColor = backgroundcolor;
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:YES];
    self.navigationController.navigationBar.hidden = YES;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self CreatView];
    
    [self shareAppVersionAlert];
}
- (void)shareAppVersionAlert {
    NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
    
    NSString *appVersion = [NSString stringWithFormat:@"v%@",infoDict[@"CFBundleShortVersionString"]];
    //
    NSDictionary * params = @{@"clientCategory":@3,@"currentVersionNumber":appVersion};
    
    WYCLog(@"--------%@",params);
    
    [HTTPRequest POST:MScheckUpdate params:params succeed:^(id responseObject) {
        WYCLog(@"--------%@",responseObject);
        if (CodeStr) {
            NSDictionary * dict = responseObject[@"data"];
            if ([dict[@"newVersion"] isEqual:@"-1"]) {
                
            }else{
                NSString * updateStatus = [NSString stringWithFormat:@"%@",responseObject[@"data"][@"newVersion"][@"updateStatus"]];
                self.updateStatus = updateStatus;
                self.versionStatus = [NSString stringWithFormat:@"%@",responseObject[@"data"][@"currentVersion"][@"versionStatus"]];
                self.updateDescription = [NSString stringWithFormat:@"%@",responseObject[@"data"][@"newVersion"][@"updateDescription"]];
                [self GupdateStatus];
            }
        }else{
            
            
        }
        
    } failure:^(NSError *error) {
        WYCLog(@"--------%@",error);
        [MBManager showBriefAlert:Failed];
    }];
    
    
    
}



- (void)GupdateStatus{
    if ([self.versionStatus intValue] != 1) {
        if ([self.updateStatus isEqualToString:@"1"]) {
            UIView * view = [[UIView alloc] init];
            view.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
            view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
            [[UIApplication sharedApplication].keyWindow  addSubview:view];
            self.backView1 = view;
            
            
            UIView * showView = [[UIView alloc] init];
            showView.frame = CGRectMake(200 * px, ScreenHeight / 2 , ScreenWidth - 400 * px, ScreenHeight / 5);
            showView.backgroundColor = [UIColor whiteColor];
            showView.layer.cornerRadius = 20 * px;
            [view addSubview:showView];
            self.showView1 = showView;
            
            
            UIButton * choseButton = [[UIButton alloc] init];
            choseButton.frame = CGRectMake(showView.width - 90 * px, 20 * px, 60 * px, 60 * px);
            [choseButton setImage:[UIImage imageNamed:@"pop_dis"] forState:UIControlStateNormal];
            [choseButton addTarget:self action:@selector(choseButtonClick) forControlEvents:UIControlEventTouchUpInside];
            [showView addSubview:choseButton];
            
            /** title*/
            UILabel * titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 50 * px, showView.width, 60 * px)];
            titleLabel.textColor = [UIColor blackColor];
            titleLabel.text = @"更新最新版本";
            titleLabel.textAlignment = NSTextAlignmentCenter;
            titleLabel.font = [UIFont systemFontOfSize:SYRealValue(35 / 2)];
            [showView addSubview:titleLabel];
            
            
            /** title*/
            UILabel * Label = [[UILabel alloc] initWithFrame:CGRectMake(10 * px, CGRectGetMaxY(titleLabel.frame) + 20 * px, showView.width, 60 * px)];
            Label.textColor = [UIColor blackColor];
            Label.text = [NSString stringWithFormat:@"%@",self.updateDescription];
            Label.textAlignment = NSTextAlignmentLeft;
            Label.font = [UIFont systemFontOfSize:SYRealValue(28 / 2)];
            [showView addSubview:Label];
            
            UIView * lineView = [[UIView alloc] initWithFrame:CGRectMake(0, showView.height / 3 * 2, showView.width, 1)];
            lineView.backgroundColor = WYCRGBColor(106, 186, 246);
            [showView addSubview:lineView];
            /** 确定*/
            UIButton * quuedingButton = [[UIButton alloc] initWithFrame:CGRectMake(100 * px, CGRectGetMaxY(lineView.frame) + 40 * px, showView.width / 2 - 200 * px, 60 * px)];
            [quuedingButton setTitle:@"确定" forState:UIControlStateNormal];
            quuedingButton.titleLabel.font = [UIFont systemFontOfSize:SYRealValue(30 / 2)];
            [quuedingButton  setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
            [quuedingButton  addTarget:self action:@selector(quuedingButtonClick) forControlEvents:UIControlEventTouchUpInside];
            [showView addSubview:quuedingButton];
            
            
            /** 确定*/
            UIButton * quxiaoButton = [[UIButton alloc] initWithFrame:CGRectMake(100 * px + showView.width / 2, quuedingButton.y, quuedingButton.width,quuedingButton.height)];
            [quxiaoButton setTitle:@"取消" forState:UIControlStateNormal];
            quxiaoButton.titleLabel.font = [UIFont systemFontOfSize:SYRealValue(30 / 2)];
            [quxiaoButton  setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
            [quxiaoButton  addTarget:self action:@selector(quxiaoButtonClick) forControlEvents:UIControlEventTouchUpInside];
            [showView addSubview:quxiaoButton];
        }else if ([self.updateStatus isEqualToString:@"2"]){
            UIView * view = [[UIView alloc] init];
            view.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
            view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
            [[UIApplication sharedApplication].keyWindow  addSubview:view];
            self.backView = view;
            
            
            UIView * showView = [[UIView alloc] init];
            showView.frame = CGRectMake(200 * px, ScreenHeight / 2 , ScreenWidth - 400 * px, ScreenHeight / 5);
            showView.backgroundColor = [UIColor whiteColor];
            showView.layer.cornerRadius = 20 * px;
            [view addSubview:showView];
            self.showView = showView;
            
            
            
            
            /** title*/
            UILabel * titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 50 * px, showView.width, 60 * px)];
            titleLabel.textColor = [UIColor blackColor];
            titleLabel.text = @"更新最新版本";
            titleLabel.textAlignment = NSTextAlignmentCenter;
            titleLabel.font = [UIFont systemFontOfSize:SYRealValue(35 / 2)];
            [showView addSubview:titleLabel];
            
            
            /** title*/
            UILabel * Label = [[UILabel alloc] initWithFrame:CGRectMake(10 * px, CGRectGetMaxY(titleLabel.frame) + 20 * px, showView.width, 60 * px)];
            Label.textColor = [UIColor blackColor];
            Label.text = [NSString stringWithFormat:@"%@",self.updateDescription];
            Label.textAlignment = NSTextAlignmentLeft;
            Label.font = [UIFont systemFontOfSize:SYRealValue(28 / 2)];
            [showView addSubview:Label];
            
            UIView * lineView = [[UIView alloc] initWithFrame:CGRectMake(0, showView.height / 3 * 2, showView.width, 1)];
            lineView.backgroundColor = WYCRGBColor(106, 186, 246);
            [showView addSubview:lineView];
            /** 确定*/
            UIButton * quuedingButton = [[UIButton alloc] initWithFrame:CGRectMake(100 * px, CGRectGetMaxY(lineView.frame) + 40 * px, showView.width / 2 - 200 * px, 60 * px)];
            [quuedingButton setTitle:@"确定" forState:UIControlStateNormal];
            quuedingButton.titleLabel.font = [UIFont systemFontOfSize:SYRealValue(30 / 2)];
            [quuedingButton  setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
            [quuedingButton  addTarget:self action:@selector(quuedingButtonClick) forControlEvents:UIControlEventTouchUpInside];
            [showView addSubview:quuedingButton];
            
            
            /** 确定*/
            UIButton * quxiaoButton = [[UIButton alloc] initWithFrame:CGRectMake(100 * px + showView.width / 2, quuedingButton.y, quuedingButton.width,quuedingButton.height)];
            [quxiaoButton setTitle:@"取消" forState:UIControlStateNormal];
            quxiaoButton.titleLabel.font = [UIFont systemFontOfSize:SYRealValue(30 / 2)];
            [quxiaoButton  setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
            [quxiaoButton  addTarget:self action:@selector(QianzhiquxiaoButtonClick) forControlEvents:UIControlEventTouchUpInside];
            [showView addSubview:quxiaoButton];
            
            
            
        }else{
            
            
        }
    }else{
        
    }
    
    
}

- (void)quxiaoButtonClick{
    
    [self choseButtonClick];
}
- (void)quuedingButtonClick{
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itms-apps://itunes.apple.com/app/id1458685882"]];
    
}
- (void)QianzhiquxiaoButtonClick{
    AppDelegate *app = [UIApplication sharedApplication].delegate;
    UIWindow *window = app.window;
    [UIView animateWithDuration:1.0f animations:^{
        window.alpha = 0;
        window.frame = CGRectMake(0, window.bounds.size.width, 0, 0);
    } completion:^(BOOL finished) {
        exit(0);
    }];
}
- (void)choseButtonClick{
    
    [self.backView1 removeFromSuperview];
}
- (void)CreatView{
    UIView * HeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 400 * px)];
    HeaderView.backgroundColor = backgroundcolor;
    [self.view addSubview:HeaderView];
    
    
    UIView * ImageView = [[UIView alloc] initWithFrame:CGRectMake(0, -20, ScreenWidth, 400 * px - 20)];
    UIImage*image = [UIImage imageNamed:@"hdzx_bg"];
    ImageView.layer.contents = (__bridge id)image.CGImage;
    /** 设置对齐模式*/
    ImageView.layer.contentsGravity = kCAGravityResize;
    [HeaderView addSubview:ImageView];
    
    
    
    
    /** title*/
    UILabel * Navtitle = [[UILabel alloc] init];
    if ([WRNavigationBar isIphoneX]) {
        Navtitle.frame = CGRectMake(ScreenWidth / 2 - 150 * px, 45, 300 * px, 60 * px);
    }else{
        Navtitle.frame = CGRectMake(ScreenWidth / 2 - 150 * px, 30, 300 * px, 60 * px);
        
    }
    Navtitle.textColor = [UIColor whiteColor];
    Navtitle.text = @"首页";
    Navtitle.textAlignment = NSTextAlignmentCenter;
    Navtitle.font = [UIFont boldSystemFontOfSize:SYRealValue(38 / 2)];
    [HeaderView addSubview:Navtitle];
    
    
    
    UIView * backView = [[UIView alloc] init];
    backView.frame = CGRectMake(60 * px, 250 * px, ScreenWidth - 120 * px, ScreenHeight - HeaderView.height);
    backView.backgroundColor = self.view.backgroundColor;
//    backView.backgroundColor = [UIColor redColor];
    [self.view addSubview:backView];
    
    
    NSArray * imageArray = @[@"财务-房间结算",@"财务-挂账结算",@"财务-会员卡充值",@"财务-其他入账"];
    
    NSArray * labelArray = @[@"房间结算",@"挂账结算",@"会员卡充值",@"其他入账"];
    int totalColumns = 2;
    
    //每行列数
    
    CGFloat W = backView.width / 2 - 30 * px;
    CGFloat H = W;
    
    //    间隙
    CGFloat margin =(backView.width - totalColumns * W) / (totalColumns + 1);
    
    
    for (int i = 0; i < imageArray.count; i++){
        
        MSTitleButton *button = [[MSTitleButton alloc ]init ];
        button.tag = i + 1000;
        [button setTitle:labelArray[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageNamed:imageArray[i]] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];
        button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        button.titleLabel.font = [UIFont boldSystemFontOfSize:SYRealValue(32 / 2)];
        int row = i / totalColumns;
        int col = i % totalColumns;
        //根据行号和列号来确定 子控件的坐标
        CGFloat cellX = margin + col * (W + margin);
        CGFloat cellY = row * (H + margin);
        button.frame = CGRectMake(cellX, 60 * px + cellY + 20 * px, W, H);
        
        // 添加到view 中
        [backView addSubview:button];
//        self.button = button;
    }
    
}

- (void)titleClick:(UIButton *)sender{
    
    if (sender.tag == 1000) {
        MSCaiwuController * BindingRelationsVC = [[MSCaiwuController alloc] init];
        [self.navigationController pushViewController:BindingRelationsVC animated:NO];
    }else if (sender.tag == 1001){
        MSFinancialAdministrationController * FinancialVC = [[MSFinancialAdministrationController alloc] init];
        [self.navigationController pushViewController:FinancialVC animated:NO];
    }else if (sender.tag == 1002){
        
        MSFinanciaActivateCardController * FinanciaActivateVC = [[MSFinanciaActivateCardController alloc] init];
        [self.navigationController pushViewController:FinanciaActivateVC animated:NO];
        
    }else{
        MSOtherEntriesController * OtherVC = [[MSOtherEntriesController alloc] init];
        [self.navigationController pushViewController:OtherVC animated:NO];
    }
}
@end
